package vn.vnu.uet.email;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;

import vn.vnu.uet.email.R;
import vn.vnu.uet.email.bean.User;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ViewContent extends DefaultActivity {
	int index;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState, R.layout.viewcontent);
		TextView title = (TextView) findViewById(R.id.title1);
		TextView content = (TextView) findViewById(R.id.content);
		index = getIntent().getIntExtra("index", -1);
		Button delete = (Button) findViewById(R.id.delete);
		delete.setText("Xoá");

		delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
					new ProgressTask(ViewContent.this).execute(null);
					
			}
		});
		try {
			title.setText(Token.instance().messages[index].getSubject());
			setTitle(convertEmail(Token.instance().messages[index].getFrom()[0].toString()));
			content.setText(Token.instance().messages[index].getContent().toString());
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

		
	protected class ProgressTask extends AsyncTask<String, Void, Boolean> {
		private ProgressDialog dialog;

		ProgressTask(Context c) {
			super();
			dialog = new ProgressDialog(c);
		}

		protected void onPreExecute() {
			this.dialog.setMessage("Đang xoá....Email \nVui lòng đợi!");
			this.dialog.show();
		}

		protected void onPostExecute(final Boolean success) {

			if (dialog.isShowing()) {
				dialog.dismiss();
				if(success)
					Toast.makeText(ViewContent.this, "Một email vừa được xoá!", Toast.LENGTH_LONG).show();
				ViewController.showInbox(ViewContent.this);
				
			}
		}

		protected Boolean doInBackground(final String... args) {
			try {
				String host = Token.instance().host;// change accordingly
				String mailStoreType = Token.instance().mailStoreType;
				String username = Token.instance().user.userName;
				String password = Token.instance().user.password;// change accordingly

				// receiveEmail(host, mailStoreType, username, password);
				Properties properties = System.getProperties();
				properties.setProperty("mail.pop3.disabletop", "true");

				// properties.put("mail.pop3.host", host);
				Session emailSession = Session.getDefaultInstance(properties);

				Store emailStore = emailSession.getStore(mailStoreType);
				try {
					emailStore.connect(host, username, password);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				Folder emailFolder = emailStore.getFolder("INBOX");
				emailFolder.open(Folder.READ_WRITE);
				System.out.println("---------------------");

				Token.instance().user = new User(null, username, null, null,
						null, username + "@vnu.edu.vn", password);
				messages = emailFolder.getMessages();
				messages[index].setFlag(Flags.Flag.DELETED, true);
				System.out.println("index " + index);

				emailFolder.close(true);
				emailStore.close();
				return true;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
	}
}
